lionsApp
    .controller('detailCtrl', ['$scope', '$state', '$rootScope', 'lionsHttp', '$stateParams', function ($scope, $state, $rootScope, lionsHttp, $stateParams) {
        $scope.teamId = $stateParams.id
        lionsHttp.detail({}, {id: $scope.teamId}, function (data) {
            $scope.detailInfo = data.data
            $scope.teamImgs = $scope.detailInfo.teamPic.split(';')
            $scope.swiper = new Swiper('.swiper-container', {
                pagination: '.swiper-pagination',
                paginationClickable: true,
                observer: true
            })
            lionsHttp.total({}, {}, function (data) {
                $scope.totalAmount = data.data.total
                $scope.totalTimes = data.data.number
            })
        })
        lionsHttp.areaList({}, {}, function (data) {
            $scope.areaList = data.data
        })
        $scope.areaChange = function () {
            $scope.manifesto = null
            if($scope.userArea == 999){
                $scope.noName = true
            }else{
                $scope.noName = false
            }
            lionsHttp.teamList({}, {area: $scope.userArea}, function (data) {
                $scope.teamList = data.data
            })
        }
        $scope.teamChange = function () {
            lionsHttp.userList({}, {team: $scope.userTeam}, function (data) {
                $scope.userList = data.data
            })
        }
        $scope.userChange = function () {
            if($scope.userList){
                $scope.userList.forEach( function (item) {
                    if(item.id == $scope.userId){
                        if(item.declaration){
                            $scope.manifesto = item.declaration
                            $scope.manifestoDefault = true
                        }else{
                            $scope.manifesto = ''
                            $scope.manifestoDefault = false
                        }
                    }
                })
            }
        }
        $scope.locationPay = function () {
            lionsHttp.updateUser({}, {
                id: $scope.userId,
                mobile: $scope.mobile,
                declaration: $scope.manifesto
            }, function (data) {
                if(data.statusCode != 0){
                    alert(data.statusMessage)
                    return
                }
                $scope.donateDisabled = false
                location.href = '/index/indexPage?type=2&channelCode=X7eP0&businessCode=E00001' + '&projectName='+ $scope.detailInfo.projectId + '&money=' + $scope.amount*100 + '&userId=' + $scope.userId + '&orderCode=11'
            })
        }
        $scope.donate = function () {
            if($scope.donateDisabled) return
            if(!$scope.manifesto){
                $scope.error = '请填写您的捐款宣言'
                return
            }
            if($scope.noName){
                if(!$scope.userName){
                    $scope.error = '请填写您的真实姓名'
                    return
                }else{
                    $scope.donateDisabled = true
                    lionsHttp.createUser({}, {
                        teamId: $scope.userTeam,
                        projectId: $scope.detailInfo.projectId,
                        name: $scope.userName
                    }, function (data) {
                        $scope.userId = data.data.id
                        $scope.locationPay()
                    })
                    return
                }
            }else{
                if(!$scope.userId){
                    $scope.error = '请填写您的真实姓名'
                    return
                }
            }
            $scope.donateDisabled = true
            $scope.error = false
            $scope.locationPay()
        }
        $scope.amount = 1000
        $scope.amountArray = [2000, 3000, 5000, 10000, 15000, 20000, 30000, 50000]
        $scope.num = 0
        $scope.amountPlus = function () {
            if($scope.num > $scope.amountArray.length - 1) return
            $scope.num++
            $scope.amount = $scope.amountArray[$scope.num - 1]
        }
        $scope.amountMinus = function () {
            if($scope.num <= 1){
                $scope.amount = 1000
                $scope.num = 0
                return
            }
            $scope.num--
            $scope.amount = $scope.amountArray[$scope.num - 1]
        }
    }])